package java6681.shopping;

import java6681.lesson01.Market6681;
import java6681.lesson01.Person6681;
import java6681.lesson01.Product6681;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

public class ShoppingWinDemo extends Application {
    private TextField tfPerson;
    private Label lblGet;

    private TextField tfBuy;

    private Market6681 mark;
    private Product6681 prod;

    @Override
    public void start(Stage primaryStage) {
//        VBox垂直布局
        VBox root = new VBox(20);
//         添加文本
        Label lblStudent = new Label("212006681|张舒婷");
//        文本边框 颜色，虚线,四角的半径，宽设置
        BorderStroke bs = new BorderStroke(Paint.valueOf("#8FBC8F")
                , BorderStrokeStyle.DOTTED
                , new CornerRadii(10)
                , new BorderWidths(3));
//        将设置添加进窗口
        lblStudent.setBorder(new Border(bs));
//        边框距离
        lblStudent.setPadding(new Insets(10));

//        HBox水平布局
        HBox hPerson = new HBox(10);
        Label lblPerson = new Label("购物者：");
//        输入框
        tfPerson = new TextField();
//        输入框里的文字
        tfPerson.setPromptText("购物者姓名");
//        添加进水平布局
        hPerson.getChildren().addAll(lblPerson, tfPerson);

        HBox hBuy = new HBox(10);
        Label lblBuy = new Label("购买商品清单：");
        tfBuy = new TextField();
        tfBuy.setPrefWidth(420);
        tfBuy.setPromptText("用逗号分隔的商品名称");
//        添加进水平布局
        hBuy.getChildren().addAll(lblBuy, tfBuy);

//          按钮
        Button button = new Button("购买");
//        按钮下的文本设置
        lblGet = new Label();

//        整体添加进VBox垂直布局中
        root.getChildren().addAll(lblStudent, hPerson, hBuy, button, lblGet);
        root.setPadding(new Insets(20.0));
//        居中
        root.setAlignment(Pos.TOP_CENTER);
//        VBox添加进场景-窗体设置宽高
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("超市购物");
        primaryStage.show();

        EventHandler<ActionEvent> listener = new MyEvenHandler();
        button.setOnAction(listener);

    }

    public String method(String str) {
        String sb = "";
        //          可乐,芬达
        Product6681 one = new Product6681("可乐");
        Product6681 two = new Product6681("芬达");
        Product6681 three = new Product6681("乌苏");
        Market6681 mark = new Market6681("永辉", new Product6681[]{two, three, one});

        String[] re = str.split(",");
        Product6681[] product = new Product6681[re.length];
        Person6681 person = new Person6681(tfPerson.getText());

        for (int i = 0; i < product.length; i++) {
            if (i != product.length - 1) {
                sb += person.shopping(mark, re[i]).getName() + ",";
            } else {
                sb += person.shopping(mark, re[i]).getName();
            }
            if (!person.shopping(mark, re[i]).getName().equals(str)) {
                continue;
            }

        }
        return sb;
    }

    class MyEvenHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            if (tfPerson.getText().equals("") && tfBuy.getText().equals("")) {
                lblGet.setText("");
            } else {
                if (method(tfBuy.getText()) == null) {
                    lblGet.setText("null");
                }
//                method(tfBuy.getText());
                lblGet.setText(tfPerson.getText() + "在永辉超市买到了：" + method(tfBuy.getText()));
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
